ActionScript实现顺序查找,二分查找

Categories: Flex; Tagged with: ; @ October 19th, 2008 16:48

写的比较潦草,欢迎指正….批评是我前进的动力……

顺序查找实现 Sequential Search

	/**
	 * 顺序查找实现 Sequential Search
	 */ 
	 public static function sequentialSearch(k:int, a:Array):int {
	 	var index:int = -1;
	 	for(var i:int = 0; i

二分查找实现

/**
	 * 二分查找实现
	 */ 
	public static function binarySearch(k:int, a:Array):int {
		var startIndex:int = 0;
		var endIndex:int = a.length - 1;
		var midIndex:int;
		var index:int = -1;
		
		while(startIndex <= endIndex) {
			midIndex = int((startIndex + endIndex)/2);
			if(k > a[midIndex]) {
				startIndex = midIndex + 1;
			}else {
				endIndex = midIndex - 1;
			}
			if(k == a[midIndex]) {
				index = midIndex;
			}
		}//end of while
		return index;
	}//end of function binarySearch

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.